package testing;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

/**
 * Created by Masha on 23.06.2014.
 */
public class Smartphone {
    @Test
    public void BuySmartphoneCase()
    {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://rozetka.com.ua/");
        driver.manage().window().maximize();
        String text = driver.findElement(By.className("m-cart-empty")).getText();
        Assert.assertEquals("Корзина пуста",text);

        Actions builder = new Actions(driver);
        builder.moveToElement(driver.findElement(By.id("phones-mp3-gps")));

        builder.perform();
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"phones-mp3-gps\"]/div/div")));//list appears

        driver.findElement(By.xpath("//*[@id=\"phones-mp3-gps\"]/div/div/ol/li[1]/ul[1]/li[1]/a")).click();
        driver.findElement(By.xpath("//*[@id=\"head_banner_container\"]/div[4]/div/div/div[2]/div[3]/ul/li[1]/ul/li[1]/a")).click();
        // all smartphones are being downloaded
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"head_banner_container\"]/div[3]/div/div[1]/div[2]/div/div[2]/h1")));

        driver.findElement(By.xpath("//*[@id=\"sort_view\"]/a")).click();//sort
        driver.findElement(By.xpath("//*[@id=\"sort_view\"]/div/div/ul/li[1]/a")).click();//sort from the cheapest
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"sort_view\"]/a")));//wait the sort ending
        String smartphone_name = driver.findElement(By.className("gtile-i-title")).getText();//the cheapest smartphone

        driver.findElement(By.className("ibutton-css-green")).click();//buy
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"cart-popup\"]/div")));//wait for cart's window
        String smartphone_name_in_basket = driver.findElement(By.xpath("//*[@id=\"cart-popup\"]/div/div/div[2]/div[2]/div[2]/div[1]/a")).getText();
        Assert.assertEquals(smartphone_name_in_basket,smartphone_name);//chek smartphone model


        // clear basket
        driver.findElement(By.name("delete")).click();//delete
        driver.quit();
    }
}
